// import { nextTick } from 'process';
import { createRouter, createWebHashHistory } from 'vue-router';
import { ElMessage } from 'element-plus';
import axios from '@/http/index';
export const constantRoutes = [
  {
    path: '/',
    redirect: '/error'
  },
  {
    path: '/restructure',
    name: 'restructure',
    component: () => import('@/views/index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Disaster',
    name: 'Disaster',
    component: () => import('@/views/Disaster_kanban.vue'),
    meta: {
      title: '灾备看板'
    },
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        localStorage.removeItem('drillSystem_radio');
        localStorage.removeItem('drillSystem_value');
        localStorage.removeItem('issue_radio');
        localStorage.removeItem('Annual_accumulation');
        localStorage.removeItem('Question_sheet');
      }
    }
  },
  // {
  //   path: '/Disaster',
  //   name: 'Disaster',
  //   component: () => import('@/views/Disaster_recovery.vue'),
  //   meta: {
  //     title: '灾备看板'
  //   }
  // },
  // {
  //   path: '/Application',
  //   name: 'Application',
  //   component: () => import('@/views/Application_level.vue'),
  //   meta: {
  //     title: '灾备看板'
  //   }
  // },
  {
    path: '/ChangeAndUpg',
    name: 'ChangeAndUpg',
    component: () => import('@/components/Formlist/ChangeAndUpg.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/dispatchList',
    // name: 'dispatchList',
    component: () => import('@/components/Details/dispatch_list.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    },
    /// redirect:{path: ''},
    children: [
      {
        path: 'Nopunctual',
        name: 'Nopunctual',
        component: () => import('@/components/DispatchList/Nopunctual.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Timeout_out_over',
        name: 'Timeout_out_over',
        component: () => import('@/components/DispatchList/Timeout_out_over.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'CurrentError',
        name: 'CurrentError',
        component: () => import('@/components/DispatchList/CurrentError.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'InOperation',
        name: 'InOperation',
        component: () => import('@/components/DispatchList/InOperation.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'startover',
        name: 'startover',
        component: () => import('@/components/DispatchList/startover.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'complete',
        name: 'complete',
        component: () => import('@/components/DispatchList/complete.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Cumulative_error',
        name: 'Cumulative_error',
        component: () => import('@/components/DispatchList/Cumulative_error.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Accumulated_timeout',
        name: 'Accumulated_timeout',
        component: () => import('@/components/DispatchList/Accumulated_timeout.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Step_Run_timeout',
        name: 'Step_Run_timeout',
        component: () => import('@/components/DispatchList/Step_Run_timeout.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      }
    ]
  },
  {
    path: '/resources',
    name: 'resources',
    component: () => import('@/components/Details/resources.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/resources_detail',
    name: 'resources_detail',
    component: () => import('@/components/detail_Information/resources_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/relationCmbiz',
    name: 'relationCmbiz',
    component: () => import('@/components/Details/relationCmbiz.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/edition',
    name: 'edition',
    component: () => import('@/components/Details/edition.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/awareness',
    name: 'awareness',
    component: () => import('@/components/Details/awareness.vue'),
    // beforeEnter: (to: any, form: any) => {
    //   if (!form.name) {
    //     router.push({
    //       name: 'restructure'
    //     });
    //   }
    // },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Annual_edition',
    name: 'Annual_edition',
    component: () => import('@/components/Details/Annual_edition.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Provincial_centre',
    name: 'Provincial_centre',
    component: () => import('@/components/Details/Provincial_centre.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/edition_detail',
    name: 'edition_detail',
    component: () => import('@/components/detail_Information/edition_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/editionTask_detail',
    name: 'editionTask_detail',
    component: () => import('@/components/detail_Information/editionTask_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Operation_list',
    name: 'Operation_list',
    component: () => import('@/components/Details/Operation_list.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/operation_detail',
    name: 'operation_detail',
    component: () => import('@/components/detail_Information/operation_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/data_monitoring',
    name: 'data_monitoring',
    component: () => import('@/components/Details/operation_Data_monitoring.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/difficlt_detail',
    name: 'difficlt_detail',
    component: () => import('@/components/detail_Information/difficlt_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/lssues_detail',
    name: 'lssues_detail',
    component: () => import('@/components/detail_Information/lssues_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/relation',
    name: 'relation',
    component: () => import('@/components/Details/relation.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/CurrentProcess',
    name: 'CurrentProcess',
    component: () => import('@/components/Details/CurrentProcess.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/CurrentProcessList',
    name: 'CurrentProcessList',
    component: () => import('@/components/Details/CurrentProcessList.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/current_processing',
    name: 'current_processing',
    component: () => import('@/components/Details/current_processing.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/monitor',
    name: 'monitor',
    component: () => import('@/components/Details/monitor.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/monitorEchart',
    name: 'monitorEchart',
    component: () => import('@/components/Details/monitorEchart.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/run_monitor',
    name: 'run_monitor',
    component: () => import('@/components/Monitot_list/run_index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Issues',
    name: 'Issues',
    component: () => import('@/components/Details/Issues_list.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/graph_monitor',
    name: 'graph_monitor',
    component: () => import('@/components/Monitot_list/graph_index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/SignGroup',
    name: 'SignGroup',
    component: () => import('@/components/Monitot_list/SignGroup.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/plan_finish_list', // 灾备演练系统列表
    name: 'plan_finish_list',
    component: () => import('@/components/Disaster/plan_finish_list.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/Question_sheet', // 所有问题单
    name: 'Question_sheet',
    component: () => import('@/components/Disaster/Question_sheet.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/Involved_system', // 问题单涉及系统
    name: 'Involved_system',
    component: () => import('@/components/Disaster/Involved_system.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/Drill_details', // 灾备系统详情
    name: 'Drill_details',
    component: () => import('@/components/Disaster_detail/Drill_details.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/monitor_detail',
    name: 'monitor_detail',
    component: () => import('@/components/detail_Information/monitor_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/UnifiedMonit',
    name: 'UnifiedMonit',
    component: () => import('@/components/UnifiedMonit/index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/error',
    name: 'error',
    component: () => import('@/components/error.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/error'
  }
];
const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes: constantRoutes
});
const getUrlParams=function(key:any) {
  const reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
  const r = window.location.search.substr(1)
      .match(reg);
  if (r != null)
      return unescape(r[2]);
  return null;
}
router.beforeEach((to, from, next) => {
  console.log(to, from);
  debugger
  if (to.name == 'UnifiedMonit') {
    const url = window.location.href.split('?')[1];
    const res = url.split('=')[1];
    const eventNo=getUrlParams('eventNo');
    const systemId=getUrlParams('systemId');
    
    if (!eventNo) {
      ElMessage({
        message: '单号不存在',
        type: 'error'
      });
      next({ name: 'error' });
    }else if(!systemId){
      ElMessage({
        message: '令牌不存在',
        type: 'error'
      });
      next({ name: 'error' });
    } else {
      console.log("--------------------------");
      console.log(to)
      if (to.name == 'UnifiedMonit') {
        axios({
          url: '/monitor/common/getToken',
          method: 'post',
          data:{}
        }).then((res) => {
          console.log(res)
          if (res.code == 200) {
            localStorage.setItem('restructureGlobalToken', res.apiToken);
            next();
          } else {
            ElMessage({
              message: res.msg,
              type: 'error'
            });
            next({ name: 'error' });
          }
        });
      } else {
        console.log("sss")
        next({
          name: 'UnifiedMonit'
        });
      }
    }
  } else if (to.name == 'restructure') {
    next();
    return;
    axios({
      url: '/monitor/common/getToken',
      method: 'post'
    }).then((res) => {
      if (res.code == 200) {
        localStorage.setItem('restructureGlobalToken', res.apiToken);
        next();
      } else {
        ElMessage({
          message: res.msg,
          type: 'error'
        });
        next({ name: 'error' });
      }
    });
  } else {
    // next();
    if (to.meta.title) {
      // document.title = to.meta.title;
      document.title = to.meta.title as string;
    }
    next();
  }
});

export default router;
